/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.controller.bjxxclassmaterialrange;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.annotation.SysLogger;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.model.BjxxClassMaterialFile;
import cn.qidisoft.edu.hzjt.model.BjxxClassMaterialRange;
import cn.qidisoft.edu.hzjt.model.Employee;
import cn.qidisoft.edu.hzjt.model.ResponseResult;
import cn.qidisoft.edu.hzjt.model.Student;
import cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialFileService;
import cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialRangeService;
import cn.qidisoft.edu.hzjt.service.ICommonService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * @author hhl
 * @version $Id: ProjectController.java, v 0.1 2018年10月23日 下午4:56:53 hhl Exp $
 */
@Controller
@RequestMapping(value = "bjxxClassMaterialRange")
public class BjxxClassMaterialRangeController {
    protected static final LoggerAdapter   LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);
    @Autowired
    private IBjxxClassMaterialRangeService bjxxClassMaterialRangeService;

    @Autowired
    private IBjxxClassMaterialFileService  bjxxClassMaterialFileService;

    @Autowired
    private ICommonService                 commonService;

    @RequestMapping(value = "/index")
    public String index(ModelMap model, HttpServletRequest request,
                        @RequestParam(value = "xmid") String xmid) {
        model.addAttribute("xmid", xmid);
        java.util.List<Employee> List = commonService.queryStudentClassByBzr(new Student());
        model.addAttribute("List", List);
        return "bjxxClassMaterialProject/selectClassTeacherList";
    }

    @RequestMapping(value = "/List")
    @ResponseBody
    public ResponseResult List(ModelMap model, HttpServletRequest request,
                               @RequestParam(value = "xmid") String xmid,
                               BjxxClassMaterialRange data) {
        try {
            data.setParentid(Integer.valueOf(xmid));
            java.util.List<BjxxClassMaterialRange> list = bjxxClassMaterialRangeService
                .queryList(data);

            String path = request.getContextPath();
            String basePath = request.getScheme() + "://" + request.getServerName() + ":"
                              + request.getServerPort() + path;

            java.util.List<BjxxClassMaterialRange> datalist = new ArrayList<BjxxClassMaterialRange>();

            if (list != null && !list.isEmpty()) {
                for (BjxxClassMaterialRange r : list) {

                    BjxxClassMaterialFile data1 = new BjxxClassMaterialFile();
                    data1.setRangeid(r.getRangeid());

                    List<BjxxClassMaterialFile> dataList = bjxxClassMaterialFileService
                        .queryList(data1);
                    //List<BjxxClassMaterialFile> filesList = new ArrayList<BjxxClassMaterialFile>();
                    String xz = "";
                    if (dataList != null && !dataList.isEmpty()) {
                        for (BjxxClassMaterialFile f : dataList) {
                            if (xz.length() > 0)
                                xz += ",";
                            xz += "&nbsp;<a href=\"" + basePath + "/DownLoadFileServlet?filename="
                                  + f.getFilename() + "&filepath=" + f.getFilepath() + "\">"
                                  + f.getFilename() + "<img src=\"" + basePath + "/img/down.png"
                                  + "\"  /></a>";

                        }
                    }
                    r.setXz(xz);
                    datalist.add(r);
                }
            }

            ResponseResult result = new ResponseResult();
            /*
             * result.setTotal(paginationBean.getTotalRecords());
             * result.setRows(paginationBean.getPageList());
             */
            result.setRows(datalist);
            //  result.setRows(list);
            return result;

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }

    /***
     * 添加指定班主任信息
     * 
     * @param request
     * @param emidList
     * @param xmid
     * @param response
     * @return
     */
    @RequestMapping(value = "/add_bzr")
    public @ResponseBody Map<String, Object> add_bzr(HttpServletRequest request,
                                                     @RequestParam String emidList,
                                                     @RequestParam(value = "xmid") String xmid,
                                                     HttpServletResponse response) {

        request.getParameterNames();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            int num = 0;
            /*
             * String[] empids = new String[emidList.split(",").length]; for (String s :
             * emidList.split(",")) { empids[num] = s; num++; }
             */
            String[] bjids = new String[emidList.split(",").length];
            for (String s : emidList.split(",")) {
                bjids[num] = s;
                num++;
            }
            BjxxClassMaterialRange bjxxClassMaterialRange = new BjxxClassMaterialRange();
            bjxxClassMaterialRange.setParentid(Integer.valueOf(xmid));
            /* bjxxClassMaterialRange.setEmpids(empids); */
            bjxxClassMaterialRange.setBjids(bjids);
            java.util.List<BjxxClassMaterialRange> list = bjxxClassMaterialRangeService
                .queryStudentClassByBzr_Range(bjxxClassMaterialRange);
            if (list != null && !list.isEmpty()) {
                for (BjxxClassMaterialRange e : list) {

                    BjxxClassMaterialRange data = new BjxxClassMaterialRange();
                    data.setParentid(Integer.valueOf(xmid));
                    data.setGid(UUIDGenerator.genReqID());
                    data.setEmpid(e.getEmpid());
                    data.setZgh(e.getZgh());
                    data.setXm(e.getXm());
                    data.setXb(e.getXb());
                    data.setBjmc(e.getBjmc());
                    data.setBjid(e.getBjid());
                    data.setFinished(true);
                    bjxxClassMaterialRangeService.add(data);
                }
            }
            dataMap.put("statua", "100");
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            dataMap.put("statua", "-99");
        }
        return dataMap;
    }

    /**
     * 
     * 全部
     * 
     * @param model
     * @param request
     * @return
     */

    @RequestMapping(value = "/add_bzr_all")
    public @ResponseBody Map<String, Object> add_bzr_all(HttpServletRequest request,
                                                         @RequestParam(value = "xmid") String xmid,
                                                         HttpServletResponse response) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            BjxxClassMaterialRange bjxxClassMaterialRange = new BjxxClassMaterialRange();
            bjxxClassMaterialRange.setParentid(Integer.valueOf(xmid));
            java.util.List<BjxxClassMaterialRange> list = bjxxClassMaterialRangeService
                .queryStudentClassByBzr_Range(bjxxClassMaterialRange);
            if (list != null && !list.isEmpty()) {
                for (BjxxClassMaterialRange e : list) {
                    BjxxClassMaterialRange data = new BjxxClassMaterialRange();
                    data.setParentid(Integer.valueOf(xmid));
                    data.setGid(UUIDGenerator.genReqID());
                    data.setEmpid(e.getEmpid());
                    data.setZgh(e.getZgh());
                    data.setXm(e.getXm());
                    data.setXb(e.getXb());
                    data.setBjmc(e.getBjmc());
                    data.setBjid(e.getBjid());
                    data.setFinished(true);
                    bjxxClassMaterialRangeService.add(data);
                }
            }
            dataMap.put("statua", "100");

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            dataMap.put("statua", "-99");
        }
        return dataMap;
    }

    @SysLogger(description = "班级项目信息", moduleName = "班级项目管理", method = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(ModelMap model, HttpServletRequest request) {
        try {
            String[] idList = request.getParameter("idList").split(",");
            String xmid = request.getParameter("xmid");
            for (String id : idList) {
                BjxxClassMaterialRange data = new BjxxClassMaterialRange();
                // data.setEmpid(Integer.valueOf(id));
                data.setBjid(Integer.valueOf(id));
                data.setParentid(Integer.valueOf(xmid));
                bjxxClassMaterialRangeService.delete(data);
            }
            //  BaseController.setParameter("msg", "删除成功");
            model.addAttribute("id", xmid);
            /* return "redirect:index"; */
            return "redirect:/bjxxClassMaterialProject/details";
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            model.addAttribute(Constant.EXPTION_MSG_KEY, e);
            return "error/error";
        }
    }
}
